/*
 * 职员视图
 * @Author: ljb
 * @Date: 2018-03-27 17:52:07
 * @Last Modified by: ljb
 * @Last Modified time: 2018-09-04 10:55:17
 */
<style lang="less" rel="stylesheet/less" type="text/less" scoped>

    .v-admin-staff-views {
        padding: 20px;
    }

</style>

<template>

	<div class="v-admin-staff-views">

		<Tabs
			type="card"
			@on-click="changeTab">

			<TabPane
				:label="$t('staff.create')"
				name="新增">

				<staff-create
					ref="create"
					:staff-roles="staff_roles"
					@post="postData"/>

			</TabPane>

			<TabPane
				:label="$t('staff.check')"
				name="查看">

				<search-form @search="confirmSearch"/>

				<staff-index
					:loading="table_loading"
					:staff-roles="staff_roles"
					:staffs="staffs.data"
					@edit="staffDetail"
					@delete="deleteStaff"
					@recovery="recoveryStaff"
					@sort="confirmSearch"/>

				<br>

				<!-- 分页组件 -->
				<Row
					type="flex"
					justify="center">
					<Page
						:total="staffs.total"
						:current="staffs.current_page"
						:page-size="query_info.per_page"
						placement="top"
						show-sizer
						show-total
						@on-change="pageChange"
						@on-page-size-change="perPageChange"/>
				</Row>

			</TabPane>
		</Tabs>

		<Modal
			v-model="show_modal"
			:title="$t('staff.update_staff')"
			class-name="vertical-middle-modal">

			<!-- 职员编辑组件 -->
			<staff-edit
				:staff-roles="staff_roles"
				:staff-detail="staff_detail"
				@update="updatestaff"/>

			<div slot="footer"/>

		</Modal>

	</div>

</template>

<script>
import qs from 'qs';
import { mapMutations } from 'vuex';

import API_LIST from 'src/API_LIST';
import staffCreate from './create';
import staffIndex from './index';
import staffEdit from './edit';
import searchForm from './search-form';

export default {

	name: 'StaffViews',

	components: {
		staffCreate,

		staffIndex,

		staffEdit,

		searchForm,
	},
	data() {
		return {
			query_info: {
				page: 1,

				per_page: 20,

				keyword: '',
			},

			search_info: {
				page: 1,

				per_page: 20,
			}, // 分页信息

			table_loading: false, // 表格更新时的loading

			show_modal: false, // 弹框的控制

			staff_detail: {}, // 职员详情

			staffs: {
				data: [],
			}, // 职员列表

			staff_roles: [], // 职员种类

		};
	},
	mounted() {
		this.fetchStaffCategory();
	},
	methods: {

		...mapMutations('spinShow', [
			'showSpin',
			'closeSpin',
		]),

		/**
         * 切换tab
         * @param  {string} tab_name {tab的name}
         * @return {undefined}
         */
		changeTab(tab_name) {
			if (tab_name === '查看') {
				this.fetchStaffList(this.query_info);
			} else {
				this.staffs.data = [];
			}
		},

		/**
         * 页码改变
         * @param  {string} pageNumber {当前页码}
         * @return {undefined}
         */
		pageChange(pageNumber) {
			this.search_info.page = pageNumber;

			this.search_info.per_page = this.query_info.per_page;

			this.fetchStaffList(this.search_info);
		},

		/**
         * 每页显示数改变
         * @param  {string} per_page {当前每页显示数}
         * @return {undefined}
         */
		perPageChange(per_page) {
			this.search_info.per_page = per_page;

			this.query_info.per_page = per_page;

			this.fetchStaffList(this.query_info);
		},

		/**
         * 确认搜索
         * @param  {Object} param {搜索的参数对象}
         * @return {undefined}
         */
		confirmSearch({ keyword }) {
			this.query_info.keyword = keyword || '';

			this.search_info = JSON.parse(JSON.stringify(this.query_info));

			this.fetchStaffList(this.query_info);
		},

		/**
         * 新增职员的post数据
		 * @param {Object} param {新增的参数}
         * @return {undefined}
         */
		postData(param) {
			this.showSpin(this.$t('staff.please_wait')); // 请稍后

			this.$http.post(API_LIST.STAFF, param)
				.then(({ data }) => {
					if (parseInt(data.code, 10) !== 0) throw new Error(data.error);

					this.$Message.success(this.$t('staff.create_success')); // '新增成功！'

					this.$refs.create.handleReset('formValidate');
				})
				.catch((err) => {
					this.$Message.error(err.message);
				})
				.then(this.closeSpin);
		},

		/**
         * 查看职员详情
         * @param  {Object} detail {当前查看的职员信息}
         * @return {undefined}
         */
		staffDetail(detail) {
			this.staff_detail = detail;

			this.show_modal = true;
		},

		/**
         * 更新职员信息
         * @param  {string} id    {需要更新的职员的id}
         * @param  {Object} param {需要更细的参数对象}
         * @return {undefined}
         */
		updatestaff(id, param) {
			this.showSpin();

			this.$http.post(`${API_LIST.STAFF}/${id}`, param)
				.then(({ data }) => {
					if (parseInt(data.code, 10) !== 0) throw new Error(data.error);

					this.$Message.success(this.$t('staff.update_success')); // '更新成功！'

					this.show_modal = false;

					this.staff_detail = {};

					this.fetchStaffList(this.search_info);
				})
				.catch((err) => {
					this.$Message.error(err.message || err.statusText);
				})
				.then(this.closeSpin);
		},

		/**
         * 恢复职员
         * @param  {Object} row {职员对象}
         * @return {undefined}
         */
		recoveryStaff(row) {
			this.table_loading = true;

			const param = {
				is_deleted: 0,
			};

			this.$http.patch(`${API_LIST.STAFF_OPERATE}/${row.id}`, param)
				.then(({ data }) => {
					if (parseInt(data.code, 10) !== 0) throw new Error(data.error);
					/* eslint-disable-next-line no-param-reassign */
					row.is_deleted = 0;

					this.$Message.success(this.$t('staff.recover_success')); // '恢复成功'
				})
				.catch((err) => {
					this.$Message.error(err.message || err.statusText);
				})
				.then(() => { this.table_loading = false; });
		},

		/**
         * 禁用职员
         * @param  {Object} row {职员对象}
         * @return {undefined}
         */
		deleteStaff(row) {
			const param = {
				is_deleted: 1,
			};

			this.table_loading = true;

			this.$http.patch(`${API_LIST.STAFF_OPERATE}/${row.id}`, param)
				.then(({ data }) => {
					if (parseInt(data.code, 10) !== 0) throw new Error(data.error);
					/* eslint-disable-next-line no-param-reassign */
					row.is_deleted = 1;

					this.$Message.success(this.$t('staff.disuse_success')); // '停用成功'
				})
				.catch((err) => {
					this.$Message.error(err.message || err.statusText);
				})
				.then(() => { this.table_loading = false; });
		},

		/**
         * 获取职员的分类
         * @return {undefined}
         */
		fetchStaffCategory() {
			this.showSpin(this.$t('staff.please_wait'));

			this.$http.get(`${API_LIST.STAFF}/create`)
				.then(({ data }) => {
					if (parseInt(data.code, 10) !== 0) throw new Error(data.error);

					this.staff_roles = data.roles;
				})
				.catch((err) => {
					this.$Message.error(err.message || err.statusText);
				})
				.then(this.closeSpin);
		},

		/**
         * 获取职员列表
         * @param  {Object} param {搜索列表的参数}
         * @return {undefined}
         */
		fetchStaffList(param) {
			this.table_loading = true;

			const query_str = qs.stringify(param);

			this.$http.get(`${API_LIST.STAFF}?${query_str}`)
				.then(({ data }) => {
					if (parseInt(data.code, 10) !== 0) throw new Error(data.error);

					this.staffs = data.panx_admin;
				})
				.catch((err) => {
					this.$Message.error(err.message || err.statusText);
				})
				.then(() => { this.table_loading = false; });
		},
	},
};
</script>
